package org.goldenport.g3.channels.facebook

import java.io.{InputStream, File}
import java.net.URL
import org.goldenport.wpath._
import org.goldenport.g3._
import org.goldenport.g3.atom._
import org.goldenport.g3.messages._
import org.goldenport.g3.adapters.{G3FileStoreAdapter, G3FileStoreDriver}

/**
 * @since   Jan.  5, 2011
 * @version Jan.  9, 2011
 * @author  ASAMI, Tomoharu
 */
class FacebookAdapter() extends G3Adapter {
  val driver = new FacebookDriver

  def createActivity(ctx: G3AgentContext) = driver.createDriverActivity(ctx)
}

class FacebookDriver extends G3Driver {
  protected def new_Agent(cmd: G3Command, ctx: G3AgentContext): FacebookDriverAgent = {
    new FacebookDriverAgent(cmd.uri, ctx)
  }
}

class FacebookDriverAgent(uri: String, context: G3AgentContext) extends G3DriverAgent(uri, context) {
  val gDataSource = context.getDataSource(uri)

  val FACEBOOK = "http://facebook.com/"
  lazy val auth = context.getAuthentication(FACEBOOK)
  // mandetory: app_id (consumer_id)
  // option: username, password
  // if username and password are none
  //   if web service
  //     ordinaly oauth procedure
  //   else if android
  //     special oauth procedure with web view
  //   else
  //     error
  // else
  //   hack oauth procedure using raw http

  //
  def get(get: Get): G3Event = {
    // XXX reconstitute GEntity
    val hget = http.GET(get.uri)
    context.httpGet(hget)
  }

  def post(post: Post): G3Event = {
    error("XXX post")
  }

  def put(put: Put): G3Event = {
    error("XXX put")
  }

  def delete(delete: Delete): G3Event = {
    error("XXX delete")
  }

  def mkcol(mkcol: Mkcol): G3Event = {
    error("XXX mkcol")
  }
}

trait FacebookContext extends G3Module {
  def facebook(atom: Symbol): G3Channel = {
    add_channel_node(atom, context.createChannel(new FacebookAdapter()))
  }
}
